﻿using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.HttpOverrides;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Hosting;
using Newtonsoft.Json;
using StarsAdmin.Core.App;
using StarsAdmin.Core.DB.InitData;
using StarsAdmin.Core.DB.SeedData;
using StarsAdmin.Web.Core.Middleware;

namespace StarsAdmin.Web.Core.Extensions
{
    public static class StartupExtensions
    {
        public static void AddExtensionsService(this IServiceCollection services, IConfiguration configuration)
        {
            // 将App配置以单例注入
            services.AddSingleton(new AppConfigs(configuration));
            // log4net配置
            services.AddLoggingConfig();
            // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
            services.AddEndpointsApiExplorer();
            services.AddSwaggerGen();
            // SQL Server配置
            services.AddDbContextConfig();
            // 跨域配置
            services.AddCorsConfig();
            // 启用本地缓存
            services.AddMemoryCache();
            // Swagger配置
            services.AddSwaggerConfig();
            // AutoMapper配置
            services.AddAutoMapperConfig();
            // Jwt配置
            services.AddJwtConfig();
            // 注入HttpContextAccessor
            services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();
            //运行的时候调用
            services.AddControllers(options =>
            {
                options.RegisterGlobalFilter();
            }).AddNewtonsoftJson(options =>
            {
                // 忽略循环引用
                options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
                // 设置时间格式
                options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
                //将属性中的空数组移出或者转换为null
                //options.SerializerSettings.Converters.Add(new NullArrayConverter<AddressOut>());
            });
        }

        public static void UseExtensionsConfigure(this IApplicationBuilder app, IWebHostEnvironment env)
        {
            AppHelper.SetServiceProvider(app.ApplicationServices);

            // 初始化数据库
            //InitSeedData.InitializeData();
            InitData.InitializeData();

            // Configure the HTTP request pipeline.
            if (env.IsDevelopment())
            {
                app.UseSwagger();
            }

            // 静态文件中间件
            app.UseStaticFiles();

            // 添加自定义的中间件
            app.UseMiddlewareException();

            // 用于设置路由
            app.UseRouting();
            //启用跨域问题
            app.UseCors(AppConfigs.Cors.AllowCors);
            // 用于将 HTTP 请求重定向到 HTTPS
            app.UseHttpsRedirection();

            app.UseForwardedHeaders(new ForwardedHeadersOptions
            {
                ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
            });
            // 先鉴权
            app.UseAuthentication();
            // 在授权
            app.UseAuthorization();
            // 用于定义控制器端点
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
    }
}